﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace FastUtil.Data
{
    public class FileUtil
    {
        public static void SaveFile(string FileFullName, byte[] FileContent)
        {
            File.WriteAllBytes(FileFullName, FileContent);
        }

        public static void SaveStringToFile(string FileFullName, string Content)
        {
            using (System.IO.StreamWriter file = new System.IO.StreamWriter(FileFullName, false, Encoding.UTF8))
            {
                //开始写入
                file.WriteLine(Content);
            }
        }

        public static string ReadFileContent(string FileFullName)
        {
            return File.ReadAllText(FileFullName, Encoding.UTF8);
        }

        /// <summary>
        /// 文件夹下所有内容copy
        /// </summary>
        /// <param name="SourcePath">要Copy的文件夹</param>
        /// <param name="DestinationPath">要复制到哪个地方</param>
        /// <param name="overwriteexisting">是否覆盖</param>
        /// <returns></returns>
        public static bool CopyDirectory(string SourcePath, string DestinationPath, bool overwriteexisting)
        {
            bool ret = false;
            try
            {
                SourcePath = SourcePath.EndsWith(@"\") ? SourcePath : SourcePath + @"\";
                DestinationPath = DestinationPath.EndsWith(@"\") ? DestinationPath : DestinationPath + @"\";

                if (Directory.Exists(SourcePath))
                {
                    if (Directory.Exists(DestinationPath) == false)
                        Directory.CreateDirectory(DestinationPath);

                    foreach (string fls in Directory.GetFiles(SourcePath))
                    {
                        FileInfo flinfo = new FileInfo(fls);
                        flinfo.CopyTo(DestinationPath + flinfo.Name, overwriteexisting);
                    }
                    foreach (string drs in Directory.GetDirectories(SourcePath))
                    {
                        DirectoryInfo drinfo = new DirectoryInfo(drs);
                        if (CopyDirectory(drs, DestinationPath + drinfo.Name, overwriteexisting) == false)
                            ret = false;
                    }
                }
                ret = true;
            }
            catch (Exception ex)
            {
                ret = false;
            }
            return ret;
        }

        /// <summary>
        /// 删除文件夹下所有内容（文件和文件夹）
        /// </summary>
        /// <param name="SourcePath"></param>
        /// <returns></returns>
        public static bool DeleteDirectoryContent(string SourcePath)
        {
            bool ret = false;
            try
            {
                if (Directory.Exists(SourcePath))
                {
                    foreach (string f in Directory.GetFileSystemEntries(SourcePath))
                    {
                        if (File.Exists(f))
                        {
                            //如果有子文件删除文件
                            File.Delete(f);
                        }
                        else
                        {
                            //循环递归删除子文件夹
                            DeleteDirectoryContent(f);
                        }
                    }
                    //删除空文件夹
                    Directory.Delete(SourcePath);
                }
                ret = true;
            }
            catch (Exception ex) // 异常处理
            {
                ret = false;
            }
            return ret;
        }
    }
}
